﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class MessageDAL
    {
        public MessageDAL(MyContext myContext)
        {
            MyContext = myContext;
        }

        public MyContext MyContext { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public int Add(Message message)
        {
            try
            {
                MyContext.Messages.Add(message);
                return MyContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int  id)
        {
            try
            {
                var del = MyContext.Messages.FirstOrDefault(x => x.Id == id);
                if (del != null)
                {
                    MyContext.Messages.Remove(del);
                }
                return MyContext.SaveChanges();                 
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public int Update(Message message)
        {
            try
            {
                var Upd = MyContext.Messages.Find(message.Id);
                if (Upd != null)
                {
                    Upd.Name = message.Name;
                    Upd.Pic = message.Pic;
                    Upd.FuWu = message.FuWu;
                    Upd.YTime = message.YTime;
                    Upd.Address = message.Address;
                    Upd.Phone = message.Phone;
                    Upd.FTime = message.FTime;
                    Upd.start = message.start;
                    Upd.XiangQing = message.XiangQing;
                }
                return MyContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反添
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Message GetMessage(int id)
        {
            try
            {
                return MyContext.Messages.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<Message> GetAll()
        {
            try
            {
                return MyContext.Messages.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Count"></param>
        /// <param name="Sum"></param>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        public List<Message> GetShow(string? name,out int Count,out int Sum,int Index=1,int Size = 2)
        {
            try
            {
                var list = MyContext.Messages.ToList();
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x=>x.Name.Contains(name)).ToList();
                }
                Count = list.Count();
                Sum = (int)Math.Ceiling(Count * 1.0 / Size);
                list = list.OrderBy(x=>x.Id).Skip((Index-1)*Size).Take(Size).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        

    }
}
